package xyz.cerberu.cet.anime.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.cerberu.cet.anime.dao.AnimeDao;
import xyz.cerberu.cet.anime.domain.Anime;
import xyz.cerberu.cet.anime.exception.AnimeUpdataException;
import xyz.cerberu.cet.global.domain.Global;
import xyz.cerberu.cet.resource.service.ResourceManagerServiceImpl;
import xyz.cerberu.cet.search.dao.SearchDao;
import xyz.cerberu.cet.user.service.CommentManagerServiceImpl;

import java.util.List;

/**
 * @author β世界
 * Created on 9:09
 * @Description 动漫管理服务的实现类
 */
@Service
public class AnimeManagerServiceImpl {
    private final Global global;
    private final AnimeDao animeDao;
    private final AnimeServiceImpl animeServiceImpl;
    private final ResourceManagerServiceImpl resourceManagerServiceImpl;
    private final CommentManagerServiceImpl commentManagerServiceImpl;
    private final SearchDao searchDao;

    @Autowired
    public AnimeManagerServiceImpl(Global global, AnimeDao animeDao, AnimeServiceImpl animeServiceImpl, ResourceManagerServiceImpl resourceManagerServiceImpl, CommentManagerServiceImpl commentManagerServiceImpl,SearchDao searchDao) {
        this.global = global;
        this.animeDao = animeDao;
        this.animeServiceImpl = animeServiceImpl;
        this.resourceManagerServiceImpl = resourceManagerServiceImpl;
        this.commentManagerServiceImpl = commentManagerServiceImpl;
        this.searchDao = searchDao;
    }

    /**
     * @author β世界 on 15:08 2021/3/15
     * 不允许删除动漫,只允许修改动漫是否显示
     * @param aniId: 动漫id
     * @return boolean
     **/
    @Deprecated
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAnime(String aniId) {
        boolean result = false;
        try{
            // 先删除评论区的相关评论
            commentManagerServiceImpl.deleteCommentByAnime(aniId);
            // 将这个动漫的所有资源都删除
            resourceManagerServiceImpl.deleteResourceByAnime(aniId);
            // 删除这个动漫
            result = animeDao.deleteAnime(aniId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @author β世界 on 9:14 2020/9/25
     * 更新一个动漫的信息
     * 1.不允许修改动漫的id
     * 2.不允许修改动漫的主要名字(可以去修改别名)，这样会导致数据和资源那边对不上，也可能导致显示出错
     * 3.允许修改这个动漫的更新日期，但是不能修改这个动漫的季度
     * 4.可以修改官网、显示图片、简介
     * @param anime: 动漫实体
     **/
    @Transactional(rollbackFor = Exception.class)
    public void updateAnime(Anime anime) {
        int line = 0;
        line = animeDao.updateAnime(anime);
        if(line != 1){
            throw new AnimeUpdataException("修改失败，修改的记录数为:"+line+"，行为异常");
        }
    }

    /**
     * @author β世界 on 20:50 2020/9/25
     * 可以通过搜索和年月份的方式来进行过滤
     * @param pageNumber: 当前页码
     * @param year: 年份
     * @param quarter: 季度
     * @param week: 周期
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    public List<Anime> getAllAnime(int pageNumber, int year, String quarter, int week) {
        // 获取过滤的动漫信息
        return animeDao.selectAnime(
                year,quarter,week,
                (pageNumber-1)* global.getPageShowNumber(),
                global.getPageShowNumber());
    }

    /**
     * @author β世界 on 20:56 2020/9/25
     * 获取某年中某个季度的动漫
     * @param year: 年份
     * @param quarter: 季度
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    public List<Anime> sameQuarter(int year,String quarter){
        return animeServiceImpl.sameYearAndQuarterAnime(year,quarter);
    }

    /**
     * @author β世界 on 20:57 2020/9/25
     * 获取一个年份中所有的动漫
     * @param year: 年份
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    public List<Anime> sameYear(int year){
        return animeServiceImpl.sameYearAnime(year);
    }

    /**
     * @author β世界 on 22:02 2020/10/9
     * 进行模糊查询,并且将返回值进行分页处理
     * @param search:搜索内容
     * @return java.util.List<com.Cet.anime.domain.Anime>0
     **/
    public List<Anime> fuzzySearch(String search) {
        return searchDao.selectFuzzyAnimeByName(search);
    }

}
